package framework.common;

import java.util.LinkedList;
import java.util.List;


/**
 * センサ応答を束ねたデバイス応答
 *
 * @author uehara
 */
public class DeviceResponse extends DeviceMessage {

	private DeviceRequest request;

	/**
	 * デバイス応答を作成する。
	 *
	 * @param request
	 *            どの要求に対する応答なのかわかるように
	 */
	public DeviceResponse(DeviceRequest request) {
		super();
		this.request = request;
	}

	public DeviceRequest getRequest() {
		return request;
	}

//	public void setRequest(DeviceRequest request) {
//		this.request = request;
//	}

	public void add(SensorResponse res) {
		super.add(res);
	}

	public List<SensorResponse> getResponses() {
		List<SensorResponse> responses = new LinkedList<SensorResponse>();
		for (SensorMessage m : getMessages()) {
			if (m instanceof SensorResponse) {
				responses.add((SensorResponse) m);
			}
		}
		return responses;
	}

	public void setResponses(List<SensorResponse> responses) {
		for (SensorResponse res : responses) {
			getMessages().add(res);
		}
	}

	public boolean isError() {
		for (SensorResponse res : getResponses()) {
			if (res.isError())
				return true;
		}
		return false;
	}

	public String getError() {
		StringBuffer sb = new StringBuffer();
		for (SensorResponse res : getResponses()) {
			if (res.isError()) {
				sb.append((String) res.getValue());
				sb.append(",");
			}
		}
		sb.delete(sb.length() - 1, sb.length());
		return sb.toString();
	}

}
